//
//  LoginViewController.m
//  EPBPro
//
//  Created by wucha on 2018/6/14.
//  Copyright © 2018年 fc. All rights reserved.
//

#import "LoginViewController.h"
#import "ResizeView.h"
#import "HttpLoginAction.h"

@interface LoginViewController ()
@property (nonatomic,strong) UITextField *userNameTextField;
@property (nonatomic,strong) UITextField *passwordTextField;
@property (nonatomic,strong) UILabel *showLabel;
@property (nonatomic,strong) UIButton *nameLoginButton;
@property (nonatomic,strong) UIButton *codeLoginButton;
@property (nonatomic,strong) UIButton *validateCodeButton;
@property (nonatomic,strong) LoginBlock loginBlock;
@end

@implementation LoginViewController

-(id)initWithLoginBlock:(LoginBlock)loginBlock{
    if (self == [super init]) {
        self.loginBlock = loginBlock;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
//    UIButton *closeButton = [UIButton new];
//    [closeButton setImage:[UIImage imageNamed:@"loginclose"] forState:UIControlStateNormal];
//    [closeButton addTarget:self action:@selector(closeButton:) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:closeButton];
//    closeButton.sd_layout.leftSpaceToView(self.view, 10).topSpaceToView(self.view, StatusBarHeight).widthIs(50).heightIs(44);
    
    ResizeView *resizeView = [[ResizeView alloc] initWithFrame:CGRectMake(0, StatusBarHeight+44, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarHeight-44)];
    [self.view addSubview:resizeView];
    ResizeViewInfo *headResizeViewInfo = [self getHeaderResizeViewInfo];
    [resizeView addResizeSubview:headResizeViewInfo];
    
    [resizeView addResizeSubview:[self getAccountResizeViewInfo]];
    [resizeView addResizeSubview:[self getPassResizeViewInfo]];
    [resizeView addResizeSubview:[self getLoginResizeViewInfo]];
}

-(void)closeButton:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(ResizeViewInfo*)getHeaderResizeViewInfo{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 100;
    
    UIButton *nameLoginButton = [UIButton new];
    nameLoginButton.titleLabel.font = [UIFont systemFontOfSize:23];
    [nameLoginButton setTitle:@"账号登录" forState:UIControlStateNormal];
    [nameLoginButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [nameLoginButton setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
    [resizeViewInfo addSubview:nameLoginButton];
    nameLoginButton.sd_layout.leftSpaceToView(resizeViewInfo.view, 20).topSpaceToView(resizeViewInfo.view, 30).widthIs(100).heightIs(45);
    [nameLoginButton addTarget:self action:@selector(nameLoginAction:) forControlEvents:UIControlEventTouchUpInside];
    self.nameLoginButton = nameLoginButton;
    self.nameLoginButton.selected = YES;
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = [UIColor grayColor];
    [resizeViewInfo addSubview:lineView];
    lineView.sd_layout.leftSpaceToView(nameLoginButton, 5).topSpaceToView(resizeViewInfo.view, 40).heightIs(25).widthIs(2);
    
    UIButton *codeLoginButton = [UIButton new];
    codeLoginButton.titleLabel.font = [UIFont systemFontOfSize:23];
    [codeLoginButton setTitle:@"验证登录" forState:UIControlStateNormal];
    [codeLoginButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [codeLoginButton setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
    [resizeViewInfo addSubview:codeLoginButton];
    self.codeLoginButton = codeLoginButton;
    codeLoginButton.sd_layout.leftSpaceToView(lineView, 5).topSpaceToView(resizeViewInfo.view, 30).widthIs(100).heightIs(45);
    [codeLoginButton addTarget:self action:@selector(codeLoginAction:) forControlEvents:UIControlEventTouchUpInside];

    self.codeLoginButton = codeLoginButton;
    
    return resizeViewInfo;
}

-(void)nameLoginAction:(id)sender{
    self.nameLoginButton.selected = !self.nameLoginButton.selected;
    self.codeLoginButton.selected = !self.codeLoginButton.selected;
    self.userNameTextField.text = @"";
    self.passwordTextField.text = @"";
    self.userNameTextField.placeholder = @"请输入手机号或账号";
    self.passwordTextField.placeholder = @"请输入密码";
    self.passwordTextField.secureTextEntry = YES;
    self.validateCodeButton.hidden = YES;
    self.passwordTextField.sd_layout.widthIs(SCREEN_WIDTH-40);
    
    [self.view endEditing:YES];
}

-(void)codeLoginAction:(id)sender{
    self.nameLoginButton.selected = !self.nameLoginButton.selected;
    self.codeLoginButton.selected = !self.codeLoginButton.selected;
    self.userNameTextField.text = @"";
    self.passwordTextField.text = @"";
    self.userNameTextField.placeholder = @"请输入手机号";
    self.passwordTextField.placeholder = @"请输入验证码";
    self.passwordTextField.secureTextEntry = NO;
    self.validateCodeButton.hidden = NO;
    self.passwordTextField.sd_layout.widthIs(SCREEN_WIDTH-140);
    [self.view endEditing:YES];

}

-(ResizeViewInfo*)getAccountResizeViewInfo{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 100;
    
    UITextField *textField = [UITextField new];
    textField.font = [UIFont systemFontOfSize:16];
    textField.placeholder = @"请输入手机号或账号";
    [resizeViewInfo addSubview:textField];
    textField.sd_layout.leftSpaceToView(resizeViewInfo.view, 20).topSpaceToView(resizeViewInfo.view, 30).widthIs(SCREEN_WIDTH-40).heightIs(30);
    self.userNameTextField = textField;
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = DefaultCellLineColor;
    [resizeViewInfo addSubview:lineView];
    lineView.sd_layout.leftSpaceToView(resizeViewInfo.view, 20).topSpaceToView(textField, 5).widthIs(SCREEN_WIDTH-40).heightIs(1);
    return resizeViewInfo;
}

-(ResizeViewInfo*)getPassResizeViewInfo{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 70;
    
    UITextField *textField = [UITextField new];
    textField.font = [UIFont systemFontOfSize:16];
    textField.secureTextEntry = YES;
    textField.placeholder = @"请输入密码";
    [resizeViewInfo addSubview:textField];
    textField.sd_layout.leftSpaceToView(resizeViewInfo.view, 20).topSpaceToView(resizeViewInfo.view, 0).widthIs(SCREEN_WIDTH-40).heightIs(30);
    self.passwordTextField = textField;
    
    UIButton *validateCodeButton = [UIButton new];
    validateCodeButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [validateCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [validateCodeButton setTitleColor:DefaultBlueColor forState:UIControlStateNormal];
    [resizeViewInfo addSubview:validateCodeButton];
    validateCodeButton.hidden = YES;
    self.validateCodeButton = validateCodeButton;
    [self.validateCodeButton addTarget:self action:@selector(validateAction:) forControlEvents:UIControlEventTouchUpInside];
    validateCodeButton.sd_layout.rightSpaceToView(resizeViewInfo.view, 20).topSpaceToView(resizeViewInfo.view, 0).heightIs(30).widthIs(100);
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = DefaultCellLineColor;
    [resizeViewInfo addSubview:lineView];
    lineView.sd_layout.leftSpaceToView(resizeViewInfo.view, 20).topSpaceToView(textField, 5).widthIs(SCREEN_WIDTH-40).heightIs(1);
    return resizeViewInfo;
}

-(void)validateAction:(id)sender{
    UIButton *button = sender;
    button.enabled = NO;
    [button setTitle:[NSString stringWithFormat:@"%ds",60] forState:UIControlStateNormal];
    [self sendValidateCode];
    [NSTimer timerWithTimeInterval:1 time:60 changeBlock:^(int value) {
        [button setTitle:[NSString stringWithFormat:@"%ds",value] forState:UIControlStateNormal];
    } completeBlock:^{
        [button setTitle:@"获取验证码" forState:UIControlStateNormal];
        button.enabled = YES;
        
    }];
}

-(void)sendValidateCode{
    if (self.userNameTextField.text.length==0) {
        [self showMessage:@"请输入手机号" autoDissmissAfterDelay:0];
        return;
    }
    SMSActionInfo *smsActionInfo = [[SMSActionInfo alloc] init];
    smsActionInfo.mobile = self.userNameTextField.text;
    smsActionInfo.module = @"verifyCode";
    [HttpLoginAction getSendSMSInfoWithSMSAction:smsActionInfo finish:^(id result, NSError *error) {
        if (!error) {
            [self showMessage:@"发送成功" autoDissmissAfterDelay:0];
        }else{
            [self showMessage:error.domain autoDissmissAfterDelay:0];

        }
    }];
    
}

-(void)showPass:(id)sender{
    UIButton *button = sender;
    button.selected = !button.selected;
    if (button.isSelected) {
        self.showLabel.text = @"隐藏";
        self.passwordTextField.secureTextEntry = NO;
    }else{
        self.showLabel.text = @"显示";
        self.passwordTextField.secureTextEntry = YES;

    }
}

-(ResizeViewInfo*)getLoginResizeViewInfo{
    ResizeViewInfo *resizeViewInfo = [[ResizeViewInfo alloc] init];
    resizeViewInfo.height = 80;
    
    UIButton *loginButton = [UIButton new];
    [loginButton setBackgroundImage:[[UIImage imageNamed:@"loginsubmit"] stretchableImageWithLeftCapWidth:30 topCapHeight:0] forState:UIControlStateNormal];
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
    [resizeViewInfo addSubview:loginButton];
    loginButton.sd_layout.leftSpaceToView(resizeViewInfo.view, 20).topSpaceToView(resizeViewInfo.view, 0).widthIs(SCREEN_WIDTH-40).heightIs(60);
    return resizeViewInfo;
}

-(void)loginAction:(id)sender{
    if (self.userNameTextField.text.length==0) {
        [self showMessage:@"请输入账号" autoDissmissAfterDelay:0];
        return;
    }
    if (self.passwordTextField.text.length==0) {
        [self showMessage:@"请输入密码" autoDissmissAfterDelay:0];
        return;
    }
    LoginActionInfo *loginActionInfo = [[LoginActionInfo alloc] init];
    if (self.nameLoginButton.selected) {
        loginActionInfo.loginMethod = @"BY_PHONE";

    }else{
        loginActionInfo.loginMethod = @"BY_SMS_CODE";

    }
    loginActionInfo.username = self.userNameTextField.text;
    loginActionInfo.password = self.passwordTextField.text;
    [self showWaitingViewWithMessage:@"登录中..." style:WaitingViewShowStyleFullSelfView];
    [HttpLoginAction postLoginInfoWithLoginAction:loginActionInfo finish:^(id result, NSError *error) {
        if (!error) {
            [HttpLoginAction getUserInfo:^(id result, NSError *error) {
                [self dismissWaitingView];
                if (!error) {
//                    [self loginSuccess];
                    if (self.loginBlock) {
                        self.loginBlock(YES);
                    }
                }
            }];

        }else{
            [self dismissWaitingView];
            [self showMessage:error.domain autoDissmissAfterDelay:0];

        }
    }];
    
}

-(void)loginSuccess{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"TabChange" object:@"2"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
